package com.tssm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tssm.common.services.BaseServiceImpl;
import com.tssm.core.util.Pages;
import com.tssm.dao.SarcQuotationCargoDAO;
import com.tssm.dao.SarcQuotationDAO;
import com.tssm.pojo.SarcQuotation;
import com.tssm.pojo.SarcQuotationCargo;
import com.tssm.pojo.SarcSupplier;
import com.tssm.service.SarcQuotationService;

@Service("quotationService")
public class SarcQuotationServiceImpl extends BaseServiceImpl<SarcQuotation> implements SarcQuotationService {
	@Resource
	private SarcQuotationDAO quotationDAO;
	@Resource
	private SarcQuotationCargoDAO quotationCargoDAO;
	
	/**保存**/
	public void saveQuotation(SarcQuotation model, List<SarcQuotationCargo> modelbody, String deleteId) {
		quotationDAO.saveOrUpdate(model);
		if(model.getCode() == null || "".equals(model.getCode())) {
			//年月日+三位流水号
			String scode = new SimpleDateFormat("yyyyMMdd").format(new Date());		
			int num = model.getQuotationid().toString().length();
			if(num < 5) {
				for(int n = 0; n < (5 - num); n++)
					scode += "0";
				scode += model.getQuotationid().toString();
			}else {
				scode += model.getQuotationid().toString().substring(5);
			}
			model.setCode(scode);
			quotationDAO.saveOrUpdate(model);
		}
		if(deleteId != null && !"".equals(deleteId)) {
			String[] ids = deleteId.split(",");
			for(String id : ids) {
				if(!"".equals(id)) {
					quotationCargoDAO.symbolDelete(SarcQuotationCargo.class, Long.parseLong(id));
				}
			}
		}
		if(modelbody != null) {
			for(SarcQuotationCargo sarcquotationcargo : modelbody) {
				if(sarcquotationcargo != null && sarcquotationcargo.getName() != null && !("").equals(sarcquotationcargo.getName())) {
					sarcquotationcargo.setDr(0L);
					sarcquotationcargo.setSupplierid(model.getSupplierid());
					sarcquotationcargo.setQuotationTime(model.getQuotationTime());
					sarcquotationcargo.setEndTime(model.getEndTime());
					sarcquotationcargo.setQuotationid(model.getQuotationid());
					quotationCargoDAO.saveOrUpdate(sarcquotationcargo);
				}
			}	
		}
	}	
	/**数量**/
	public Integer queryQuotationCount(SarcSupplier ssup) {
		String hql = "select count(*) from SarcQuotation model where model.supplierid="+ssup.getSupplierid()+" and model.dr=0";
		Integer count = quotationDAO.getCount(hql);
		return count;
	}
	/**列表**/
	public List<SarcQuotation> queryQuotation(SarcSupplier ssup, Pages pages) {
		String hql = "from SarcQuotation model where model.supplierid="+ssup.getSupplierid()+" and model.dr=0";
		List<SarcQuotation> modelList = quotationDAO.find(hql, pages.getFirstIndex(), pages.getPageSize());
		return modelList;
	}	
	/**删除**/
	public void deleteQuotation(Long id) {
		quotationDAO.symbolDelete(SarcQuotation.class, id);
		String hql = "from SarcQuotationCargo squocargo where squocargo.quotationid="+id+" and squocargo.dr=0";
		List<SarcQuotationCargo> modelbody = quotationCargoDAO.find(hql);
		if(modelbody != null && modelbody.size() > 0) {
			for(SarcQuotationCargo cargo: modelbody) {
				quotationCargoDAO.symbolDelete(SarcQuotationCargo.class, cargo.getQuocargoid());
			}
		}
	}
	/**根据主表ID查询列表**/
	public List<SarcQuotationCargo> queryQuotationCargo(Long id) {
		String hql = "from SarcQuotationCargo squocargo where squocargo.quotationid="+id+" and squocargo.dr=0";
		List<SarcQuotationCargo> modelbody = quotationCargoDAO.find(hql);
		return modelbody;
	}
	
}
